/**
 * Checks that required fields are filled.
 */
function validateEmployeeInsert() {
    
    //Record all form values.
    var fName = document.getElementById("fName").value;
    var lName = document.getElementById("lName").value;
    var pNumber = document.getElementById("pNumber").value;
    var address = document.getElementById("address").value;
    var hRate = document.getElementById("hourlyRate").value;
    var eType = document.getElementById("employeeType").value;
    var newEType = document.getElementById("newEType").value;
    var newETypeDesc = document.getElementById("newETypeDesc").value;
    
    //Check that these fields are filled.
    if (fName == ""
        || lName == ""
        || pNumber == ""
        || address == ""
        || hRate == "") {
        
        alert ("Please fill all required fields.");
        return false;
    }
    
    //If there is a new employee type, we must have a desc and use it.
    if (newEType != "" && newETypeDesc == "") {
        
        alert("To add a new employee type, please add a description.");
        return false;
    } else {
        
        //This case means that we will not add a new E_Type.
        return true;
    }
}

/**
* Checks that the required fields are filled. 
*/
function validateServiceInsert() {
    
    //Record all form values.
    var name = document.getElementById("name").value;
    var rate = document.getElementById("rate").value;
    
    //Check that these fields are filled.
    if (name == "" || rate == "") {
        
        alert("Please fill all required fields.");
        return false;
    } else {
        
        return true;
    }
}

/**
 * Checks that the required fields are filled.
 */
function validateInventoryInsert() {
    
    //Record all form values.
    var model = document.getElementById("model").value;
    var color = document.getElementById("color").value;
    var year = document.getElementById("year").value;
    var price = document.getElementById("price").value;
    
    //Check that these fields are filled.
    if (model == ""
        || color == ""
        || year == ""
        || price == "") {
        
        alert("Please fill all required fields.");
        return false;
    } else {
        
        return true;
    }
}

/**
 * Checks that the required fields are filled.
 */
function validateSalesInsert() {
    
    //Record values
    var commission = document.getElementById("commission").value;
    var total = document.getElementById("total").value;
    
    //Check that it is filled.
    if (commission == "" || total == "") {
        
        alert("Please fill all required fields.");
        return false;
    } else {
        
        return true;
    }
}

/**
 * Displays drop down menus one at a time for each extra.
 */
function addExtraOption() {
    
    //Access nodes.
    var parent = document.getElementById("extras");
    var selectBoxes = parent.getElementsByTagName("select");
    var triggers = parent.getElementsByTagName("input");
    
    for (var i = 0; i < selectBoxes.length; i++) {
        
        //Hide "NONE" Show Remove button.
        if (i == 0) {
            
            document.getElementById("noneLabel").setAttribute("style", "display: none");
            document.getElementById("removeExtra").setAttribute("style", "display: inline");
        }
        
        //If not visible.
        if (selectBoxes[i].getAttribute("style") == "display: none") {
            
            //Make it visible, set trigger to true.
            selectBoxes[i].setAttribute("style", "display: inline");
            triggers[i].setAttribute("value", "true");
            break;
        }
    }
}

/**
 * Hides drop down menu for extras.
 */
function removeExtraOption() {
    
    //Access nodes.
    var parent = document.getElementById("extras");
    var selectBoxes = parent.getElementsByTagName("select");
    var triggers = parent.getElementsByTagName("input");
    
    for (var i = selectBoxes.length - 1; i >= 0; i--) {
        
        //If the last one is visible
        if (selectBoxes[i].getAttribute("style") != "display: none") {
            
            //If we are erasing the last one.
            if (i == 0) {
                
                //Show None, hide delete.
                document.getElementById("noneLabel").setAttribute("style", "display: inline");
                document.getElementById("removeExtra").setAttribute("style", "display: none");
            }
            
            //Set it invisible. Set trigger to false.
            selectBoxes[i].setAttribute("style", "display: none");
            triggers[i].setAttribute("value", "false");
            break;
        }
    }
}

/**
 * Displays drop down menus one at a time for each extra.
 */
function validateWashInsert() {
    
    //Access nodes.
    var parent = document.getElementById("extras");
    var selectBoxes = parent.getElementsByTagName("select");
    var triggers = parent.getElementsByTagName("input");
    
    //Iterate over each node comparing with all other nodes.
    for (var i = 0; i < selectBoxes.length; i++) {
        
        //Only evaluate if true. Useless otherwise.
        if (triggers[i].getAttribute("value") == "true") {
            
            //Second iteration for comparison.
            for (var j = 0; j < selectBoxes.length; j++) {
                
                if (triggers[j].getAttribute("value") == "false") {
                    
                    //No more will be true after this.
                    break;
                }
                
                //If value is true, and they equal, BUT i != J then we have a problem.
                if (selectBoxes[i].value == selectBoxes[j].value
                    && i != j) {
                    
                    alert("You may only select each extra option once.");
                    return false;
                }
            }
        } else {
            
            //If we get to a false i trigger and we get here, we have seen all selected options and we are ok.
            return true;
        }
    }
    
    //Case where all are chosen and there are no doubles.
    return true;
}

/**
 * Checks that required fields are filled.
 */
function validateExpenseInsert() {
    
    //Record all form values.
    var cost = document.getElementById("cost").value;
    var expenseType = document.getElementById("expenseType").value;
    var newEType = document.getElementById("newEType").value;
    var occurrence = document.getElementById("occurrence").value;
    
    //Check that these fields are filled.
    if (cost == "") {
        
        alert ("Please fill all required fields.");
        return false;
    } else if (newEType.length > 40) {
        
        alert("Maximum field length is 40. Please use a shorter description.");
        return false;
    } else if (cost < 0 || cost > 9999) {
        
        alert("Cost must be less than $9999.");
        return false;
    } else {
        
        return true;
    }
}
/**
 * Checks that all fields are filled
 */
function validateScheduleInsert() {
    
    //Record all form values
    var sTime = document.getElementById("sTime").value;
    var eTime = document.getElementById("eTime").value;
    var date = document.getElementById("date").value;
    var tips = document.getElementById("tips").value;
    
    var timeRegex = /^\d{2}:\d{2}:\d{2}$/;
    var dateRegex = /^\d{4}-\d{2}-\d{2}$/;
    
    if (sTime == ""
        || eTime == ""
        || date == ""
        || tips == "") {
        
        alert("Please fill all required fields.");
        return false;
    } else if (sTime.match(timeRegex) == null || eTime.match(timeRegex) == null) {
        
        alert("Please enter times in the format ##:##:##");
        return false;
    } else if (date.match(dateRegex) == null) {
        
        alert("Please enter date in the format: YYYY-MM-DD");
        return false;
    }
    
    //Parse times.
    var times1 = sTime.split(":");
    
    if (times1[0] < 0 || times1[0] > 24) {
        
        alert("Hours must be between 0 and 23.");
        return false;
    } else if (times1[1] < 0 || times1[1] > 59 || times1[2] < 0 || times1[2] > 59) {
        
        alert("Minutes and seconds must be between 0 and 59.");
        return false;
    }
    
    var times2 = eTime.split(":");
    
    if (times2[0] < 0 || times2[0] > 24) {
        
        alert("Hours must be between 0 and 23.");
        return false;
    } else if (times2[1] < 0 || times2[1] > 59 || times2[2] < 0 || times2[2] > 59) {
        
        alert("Minutes and seconds must be between 0 and 59.");
        return false;
    } else {
        
        return true;
    }
}